package com.kadapatha.web.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kadapatha.service.api.user.UserService;
import com.kadapatha.service.exception.ServiceException;

@Controller
@RequestMapping("/login")
public class AuthanticationController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String getCreateForm(Model model) {
	model.addAttribute(new UserForm());
	return "/login";
    }

    @RequestMapping(value = "/authanticate", method = RequestMethod.POST)
    public @ResponseBody
    boolean authanticate(@RequestParam String userName,
	    @RequestParam String password) {
	try {
	    return userService.authanticateUser(userName, password);
	} catch (ServiceException e) {
	    return false;
	}
    }

}
